package com.sdy.dao.mysql;

import com.sdy.dao.mysql.provider.NoticProvider;
import com.sdy.po.Notic;
import com.sdy.po.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NoticMapper {

    @Insert("insert into po_notic(companyId,title,info,data,noticState,readState,Time,parmId,userId) " +
            "values(#{notic.companyId},#{notic.title},#{notic.info},#{notic.data},#{notic.noticState},#{notic.readState},#{notic.Time},#{notic.parmId})")
    Boolean insertNotic(@Param("notic") Notic notic);

    @Update("update po_notic set readState = #{readState} where id = #{id}")
    void markAsRead(@Param("id") Integer noticId,@Param("readState") String readState);

    /**
     * 根据id获取notic
     * @param id
     * @return
     */
    @Select("select companyId,title,info,data,noticState,readState,Time,parmId,userId from po_notic where id = #{id}")
    @Results({
            @Result(column = "parmId", property = "parm", one = @One(select = "com.sdy.dao.mysql.ParmMapper.selectById")),
            @Result(column = "userId", property = "user",one = @One(select = "com.sdy.dao.mysql.UserMapper.getNameById")),
            @Result(column = "companyId", property = "companyName", one = @One(select = "com.sdy.dao.mysql.CompanyMapper.getCompanyNameById"))
    })
    Notic getOneById(@Param("id") Integer id);

    @Select("select id,title,Time,noticState,readState from po_notic where companyId = #{companyId}")
    List<Notic> getListByCompanyId(@Param("companyId") Integer companyId);

    @Select("select id,title,Time,noticState,readState,companyId from po_notic")
    @Results({
            @Result(column = "companyId", property = "companyName", one = @One(select = "com.sdy.dao.mysql.CompanyMapper.getCompanyNameById")),
            @Result(column = "userId", property = "user",one = @One(select = "com.sdy.dao.mysql.UserMapper.getNameById")),
    })
    List<Notic> getAll();

    @InsertProvider(type = NoticProvider.class, method = "batchInsert")
    boolean batchInsert(@Param("notic") Notic notic,@Param("receiveUserList") List<User> receiveUserList);


}
